﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraTabbedMdi;

namespace APPLICATION.UserManager
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }

        UMdataAccess DataAccess = new UMdataAccess();
        FrmAddEditUser FrmAddEditUsers1;
        FrmPages FrmPages1;
        DataView dv;
        //TreeList's selected item ID
        string ID;
        //Just for user
        string StaffID;
        //Just for action
        string mgrid;
        private int oldComboBoxItemIndex;

        private void FrmMain_Load(object sender, EventArgs e)
        {
            Form1 frm = new Form1();
            frm.ShowDialog();
            isDefaultText = true;
            DataAccess.fillStUsers();
            DataAccess.fillstRoles();
            DataAccess.fillstActions();
            DataAccess.filldtStaff();

            toolStrip1.ImageList = imageList1;
            if (FrmPages1 == null)
            {
                FrmPages1 = new FrmPages();
            }

            FrmPages1.MdiParent = this;
            FrmPages1.Show();

            imageComboBoxEdit1.SelectedIndex = 0;            


        }
        

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int newComboBoxItemIndex = imageComboBoxEdit1.SelectedIndex;
            if (FrmPages1.isEdited())
            {
                counter += 1;
                imageComboBoxEdit1.SelectedIndex = oldComboBoxItemIndex;
                if (counter == 2)
                {
                    counter = 0;
                    return;
                }
                if (FrmPages1.myMessageAndOperation() == false)
                {
                    return;
                }
                else
                {
                    imageComboBoxEdit1.SelectedIndex = newComboBoxItemIndex;
                }
                
            }
            
            oldComboBoxItemIndex = imageComboBoxEdit1.SelectedIndex;
            toolStripButtonAdd.ImageIndex = imageComboBoxEdit1.Properties.Items[imageComboBoxEdit1.SelectedIndex].ImageIndex;            
            treeListColumn2.Caption = "List of " + imageComboBoxEdit1.EditValue.ToString();
            string SelectedText="";            
            switch (imageComboBoxEdit1.SelectedIndex)
            {                    
                case 0:
                     dv = new DataView(GlobalDataFile.UserManagerDataSet.stUsers);                    
                    treeList2.DataSource = dv;
                    treeListColumn2.FieldName = "UserN";
                    treeList2.KeyFieldName = "UserID";
                    treeList2.ParentFieldName = "UserID";
                    treeList2.PreviewFieldName = "UserN";
                    SelectedText = imageComboBoxEdit1.EditValue.ToString();                    
                    break;
                case 1:                  
                     dv = new DataView(GlobalDataFile.UserManagerDataSet.stRoles);
                    treeList2.DataSource = dv;
                    treeListColumn2.FieldName = "RolesN";
                    treeList2.KeyFieldName = "RolesID";
                    treeList2.ParentFieldName = "RolesID";
                    treeList2.PreviewFieldName = "RolesN";
                    SelectedText = imageComboBoxEdit1.EditValue.ToString();
                    break;
                case 2:
                     dv = new DataView(GlobalDataFile.UserManagerDataSet.stActions);
                     treeList2.DataSource = dv;
                     treeListColumn2.FieldName = "hName";
                     treeList2.KeyFieldName = "CodeID";
                     treeList2.ParentFieldName = "mgrid";
                     treeList2.PreviewFieldName = "hName";
                    
                    SelectedText = imageComboBoxEdit1.EditValue.ToString();
                    break;
            }

            FrmPages1.VisibleTabs(SelectedText);

            if (Convert.ToBoolean(treeList2.Nodes.Count))
            {
                treeList2.SetFocusedNode(treeList2.Nodes[0]);                                
            }

            if (treeList2.Selection.Count == 0)
            {
                FrmPages1.SelectedID = null;
            }
            
        }

        private void treeList2_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {            
            toolStripButtonEdit.Enabled = Convert.ToBoolean(treeList2.Selection.Count);
            toolStripButtonDelete.Enabled = Convert.ToBoolean(treeList2.Selection.Count);

            if (treeList2.Selection.Count == 0)
            {
                toolStripLabel1.Text = "Not Selected";
                FrmPages1.SelectedID = null;
            }
            else
            {
                toolStripLabel1.Text = imageComboBoxEdit1.EditValue.ToString().Remove(imageComboBoxEdit1.EditValue.ToString().Length-1,1) + ":  " + treeList2.Selection[0].GetValue(treeListColumn2.FieldName).ToString();
            }
            
        }

        private int counter;
        private bool shevides=true;
        private DevExpress.XtraTreeList.Nodes.TreeListNode oldNode;
        private void treeList2_BeforeFocusNode(object sender, DevExpress.XtraTreeList.BeforeFocusNodeEventArgs e)
        {
            if (FrmPages1.isEdited() && shevides )
            {
                counter += 1;
                FrmPages1.myMessageAndOperation();
                oldNode = e.OldNode;
                return;
            }
            else if (shevides == false)
            {
                counter += 1;
            }
        }

        private void treeList2_AfterFocusNode(object sender, DevExpress.XtraTreeList.NodeEventArgs e)
        {
            if (FrmPages1.isEdited())
            {
                if (counter == 1)
                {
                    shevides = false;
                }
                else if (counter == 2)
                {
                    shevides = true;
                    counter = 0;
                }
                treeList2.FocusedNode = oldNode;
                return;
            }
  

            if (treeList2.Selection.Count == 0)
            {
                FrmPages1.SelectedID = null;
                return;
            }

            switch (imageComboBoxEdit1.SelectedIndex)
            {
                case 0:
                    ID = e.Node.GetValue("UserID").ToString();
                    StaffID = e.Node.GetValue("StaffID").ToString();
                    break;
                case 1:
                    ID = e.Node.GetValue("RolesID").ToString();
                    break;
                case 2:
                    ID = e.Node.GetValue("CodeID").ToString();
                    mgrid = e.Node.GetValue("CodeID").ToString();
                    break;
                default:
                    ID = "0";
                    break;
            }

            FrmPages1.SelectedID = ID;
        }

        private void toolStripButtonEdit_Click(object sender, EventArgs e)
        {
            if (FrmPages1.isEdited())
            {
                if (FrmPages1.myMessageAndOperation()==false)
                {
                    return;
                }
            }

            if (treeList2.Selection.Count == 0)
            {
                return;
            }
            switch (imageComboBoxEdit1.EditValue.ToString())
            {
                case "Users":
                    FrmAddEditUsers1 = new FrmAddEditUser();
                    FrmAddEditUsers1.ForEditData = true;
                    FrmAddEditUsers1.UserID = Convert.ToInt16(ID);
                    FrmAddEditUsers1.StaffID = Convert.ToInt16(StaffID);
                    FrmAddEditUsers1.ShowDialog(this);
                    break;
                case "Roles":
                    FrmAddEditRole FrmAddEditRole1 = new FrmAddEditRole();
                    FrmAddEditRole1.ForEdit = true;                    
                    FrmAddEditRole1.RolesID = ID;       
                    FrmAddEditRole1.ShowDialog(this);
                    break;
                case "Actions":
                    FrmAddEditAction FrmAddEditAction1 = new FrmAddEditAction();
                    FrmAddEditAction1.editData = true;                    
                    FrmAddEditAction1.CodeID = ID;
                    FrmAddEditAction1.ShowDialog(this);
                    break;
            }
        }

        private void toolStripButtonAdd_Click(object sender, EventArgs e)
        {
            if (FrmPages1.isEdited())
            {
                if (FrmPages1.myMessageAndOperation() == false)
                {
                    return;
                }
            }

            switch (imageComboBoxEdit1.EditValue.ToString())
            {
                case "Users":
                    FrmAddEditUsers1 = new FrmAddEditUser();
                    FrmAddEditUsers1.ShowDialog(this);
                    break;
                case "Roles":
                    FrmAddEditRole FrmAddEditRole1 = new FrmAddEditRole();
                    FrmAddEditRole1.ShowDialog(this);
                    break;
                case "Actions":
                    FrmAddEditAction FrmAddEditAction1 = new FrmAddEditAction();
                    //sezgudvaa meotxe levelis shemdeg....
                    FrmAddEditAction1.mgrid = mgrid;
                    FrmAddEditAction1.ShowDialog(this);
                    break;
            }
        }

        private void toolStripButtonDelete_Click(object sender, EventArgs e)
        {
            if (FrmPages1.isEdited())
            {
                if (FrmPages1.myMessageAndOperation() == false)
                {
                    return;
                }
            }

            DialogResult dlr= MessageBox.Show(this, "Вы хотите удалить?", "User manager", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (dlr == DialogResult.No)
            {
                return;
            }
            switch (imageComboBoxEdit1.EditValue.ToString())
            {
                case "Users":
                    DataAccess.deleteUser(Convert.ToInt16(ID));
                    break;
                case "Roles":
                    DataAccess.deleteRole(Convert.ToInt16(ID));
                    break;
                case "Actions":
                    DataAccess.deleteAction(Convert.ToInt16(ID));                    
                    break;
            }
            treeList2.CollapseAll();
        }

        #region Look for in tree
        bool isDefaultText;
        private void textBoxSearch_Enter(object sender, EventArgs e)
        {
            if (isDefaultText)
            {
                //dv.RowFilter = "";
                textBoxSearch.Text = "";
                textBoxSearch.ForeColor = Color.Black;
            }
        }

        private void textBoxSearch_Leave(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBoxSearch.Text.Trim()))
            {
                dv.RowFilter = "";
                textBoxSearch.Text = "Search by name";
                textBoxSearch.ForeColor = Color.Gray;
                isDefaultText = true;
            }
            else
            {
                isDefaultText = false;

            }
        }

        bool isKeyPressed;
        private void textBoxSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (FrmPages1.isEdited())
            {
                if (FrmPages1.myMessageAndOperation() == false)
                {
                    textBoxSearch.Text = "";
                    return;
                }
                else
                {
                    isKeyPressed = true;
                    textBoxSearch_TextChanged(sender, e); }
            }            
            isKeyPressed = true;
        }

        private void textBoxSearch_TextChanged(object sender, EventArgs e)
        {
            if (isKeyPressed)
            {
                dv.RowFilter = treeListColumn2.FieldName.ToString() + " like '%" + textBoxSearch.Text + "%'";                
            }
            isKeyPressed = false;
        }
        #endregion

       // Midi  Manager  :)
        public class StrippedTabbedMdi : XtraTabbedMdiManager
        {
            public StrippedTabbedMdi()
            { }
            public StrippedTabbedMdi(IContainer c) : base(c) { }
            protected override void PatchActiveChild()
            {
            }
            protected override void PatchBeforeActiveChild(Form activatedForm)
            {

            }
            protected override void RegisterMDIChild(Form child)
            {
                if (child == null)
                    return;
                XtraMdiTabPage newPage = CreateNewPage(child);                
                Pages.Add(newPage);                
                OnPageAdded(new MdiTabPageEventArgs(newPage));

            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
                        
        }

        private void imageComboBoxEdit1_Properties_BeforeShowMenu(object sender, DevExpress.XtraEditors.Controls.BeforeShowMenuEventArgs e)
        {

        }



    }
}
